package com.quwan.common.service.impl.ums;

import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import com.quwan.common.enums.StatusEnum;
import com.quwan.common.form.JpushForm;
import com.quwan.common.form.PushForm;
import com.quwan.common.result.PushDTO;
import com.quwan.common.service.push.MsgSender;
import com.quwan.config.yml.PushConfig;
import com.quwan.utils.JpushUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class IosUMSMessage implements MsgSender {
    @Override
    public PushDTO pushAll(PushForm pushForm, PushConfig pushConfig) {
        JpushUtil.createClient(pushConfig.getJums().getAppKey(), pushConfig.getJums().getMasterSecret());
        JpushForm form = new JpushForm(pushForm.getTitle(),
                pushForm.getContent(),pushForm.getContent(),
                pushForm.getExtrasMap(),
                pushConfig.getJums().getEnvironment());
        PushResult pushResult = JpushUtil.pushAndroidAndIos(form);
        if (pushResult != null){
            if (pushResult.isResultOK()){
                return new PushDTO(String.valueOf(pushResult.msg_id),StatusEnum.SUCEESS.getCode());
            }
        }
        return new PushDTO("",StatusEnum.FAIL.getCode());
    }

    @Override
    public PushDTO pushToken(PushForm pushForm, PushConfig pushConfig) {
        log.info("推送别名：" + pushForm.getAlias());
        JpushUtil.createClient(pushConfig.getJums().getAppKey(), pushConfig.getJums().getMasterSecret());
        JpushForm form = new JpushForm(pushForm.getAlias(),pushForm.getTitle(),
                pushForm.getContent(),pushForm.getContent(),
                pushForm.getExtrasMap(),Platform.ios(),
                pushConfig.getJums().getEnvironment());
        PushResult pushResult = JpushUtil.pushAliasAlertWithTitle(form);
        if (pushResult != null){
            if (pushResult.isResultOK()){
                return new PushDTO(String.valueOf(pushResult.msg_id),StatusEnum.SUCEESS.getCode());
            }
        }
        return new PushDTO("",StatusEnum.FAIL.getCode());
    }

}
